/*
 * @Author: muuzi@163.com muuzi@163.com
 * @Date: 2024-12-04 22:56:27
 * @LastEditors: muuzi@163.com muuzi@163.com
 * @LastEditTime: 2024-12-15 22:00:21
 * @FilePath: /mp-wechat/src/utils/request.ts
 * @Description: 这是默认设置,请设置`customMade`, 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
 */

// 添加请求拦截器
const request = (config: any)=>{
    return new Promise((resolve, reject) => {

        // 请求地址携带时间戳
        const _t = new Date().getTime()
        config.url += `?${_t}`
        config.headers = config.headers || {}
        // 请求头携带token
       try {
            let token  = uni.getStorageSync('App-Token') || ''
            if (token) {
                config.headers["Authorization"] = "Bearer " + token; // 让每个请求携带自定义token 请根据实际情况自行修改
              }
       } catch (error) {
            console.log('config.headers',error)
       }
        uni.request({
            url:'http://localhost:80'+config.url, //仅为示例，并非真实接口地址。
            method: config.method,
            data:config.data,
            params:config.params,
            timeout: 20000,
            header:  config.headers,
            success: (response) => {
                console.log("response===",response);
                if (response.data.code === 0) {
                  resolve(response.data)
                } else {
                    let error = response
                    // 对响应错误做点什么
                    if (error && error.response) {
                        switch (error.response.code) {
                            case 400:
                                error.msg = '错误请求';
                                break;
                            case 401:
                                error.msg = '未授权，请重新登录';
                                break;
                            case 403:
                                error.msg = '拒绝访问';
                                break;
                            case 404:
                                error.msg = '请求错误,未找到该资源';
                                break;
                            case 405:
                                error.msg = '请求方法未允许';
                                break;
                            case 408:
                                error.msg = '请求超时';
                                break;
                            case 500:
                                error.msg = '服务器端出错';
                                break;
                            case 501:
                                error.msg = '网络未实现';
                                break;
                            case 502:
                                error.msg = '网络错误';
                                break;
                            case 503:
                                error.msg = '服务不可用';
                                break;
                            case 504:
                                error.msg = '网络超时';
                                break;
                            case 505:
                                error.msg = 'http版本不支持该请求';
                                break;
                            default:
                                error.msg = `未知错误${error.response.code}`;
                        }
                    } else {
                        error.msg = "连接到服务器失败";
                    }
                    reject(error);
                }
            }
        });
    })

}
// 对外暴露请求方法
export default request